// 模拟新闻数据
const newsData = {
    carousel: [
        {
            id: 1,
            title: "科技创新引领未来：人工智能新突破",
            summary: "最新研究表明，人工智能技术在医疗诊断领域取得重大突破，准确率达到95%以上。",
            image: "https://via.placeholder.com/1200x400/3498db/ffffff?text=AI+Breakthrough",
            category: "科技"
        },
        {
            id: 2,
            title: "全球经济复苏态势明显，中国市场表现亮眼",
            summary: "最新经济数据显示，全球主要经济体呈现复苏态势，中国市场增长率达到5.2%。",
            image: "https://via.placeholder.com/1200x400/2ecc71/ffffff?text=Global+Economy",
            category: "国际"
        },
        {
            id: 3,
            title: "体育盛事：奥运会筹备工作进入冲刺阶段",
            summary: "距离奥运会开幕还有100天，各项筹备工作正在紧锣密鼓地进行中。",
            image: "https://via.placeholder.com/1200x400/e74c3c/ffffff?text=Olympics+2024",
            category: "体育"
        }
    ],
    news: [
        {
            id: 4,
            title: "国内首个量子计算云平台正式上线",
            summary: "该平台将为科研机构和企业提供量子计算服务，标志着我国在量子计算领域迈出重要一步。",
            image: "https://via.placeholder.com/400x200/9b59b6/ffffff?text=Quantum+Computing",
            category: "科技",
            time: "2024-01-15 09:30"
        },
        {
            id: 5,
            title: "新能源汽车销量创新高，同比增长35%",
            summary: "最新统计显示，新能源汽车市场持续火热，消费者对环保出行方式的接受度不断提高。",
            image: "https://via.placeholder.com/400x200/1abc9c/ffffff?text=New+Energy+Cars",
            category: "国内",
            time: "2024-01-15 08:45"
        },
        {
            id: 6,
            title: "国际空间站迎来新一批宇航员",
            summary: "三名宇航员成功对接国际空间站，将开展为期6个月的科学实验任务。",
            image: "https://via.placeholder.com/400x200/34495e/ffffff?text=Space+Station",
            category: "国际",
            time: "2024-01-15 07:20"
        },
        {
            id: 7,
            title: "CBA联赛：广东队逆转取胜",
            summary: "在昨晚进行的CBA常规赛中，广东队在落后20分的情况下完成逆转，以108:105战胜对手。",
            image: "https://via.placeholder.com/400x200/f39c12/ffffff?text=CBA+Game",
            category: "体育",
            time: "2024-01-15 06:15"
        },
        {
            id: 8,
            title: "5G网络覆盖率达到95%，用户体验大幅提升",
            summary: "工信部最新数据显示，我国5G网络建设取得显著成效，用户平均下载速度提升3倍。",
            image: "https://via.placeholder.com/400x200/16a085/ffffff?text=5G+Network",
            category: "科技",
            time: "2024-01-15 05:30"
        },
        {
            id: 9,
            title: "春节旅游市场预订火爆，热门线路一票难求",
            summary: "距离春节还有两周，各大旅游平台数据显示，国内游预订量同比增长150%。",
            image: "https://via.placeholder.com/400x200/e67e22/ffffff?text=Spring+Festival+Travel",
            category: "国内",
            time: "2024-01-15 04:45"
        }
    ]
};

// 轮播图功能
let currentSlide = 0;
const totalSlides = newsData.carousel.length;

function initCarousel() {
    const carouselInner = document.querySelector('.carousel-inner');
    const indicators = document.getElementById('carousel-indicators');
    
    // 清空现有内容
    carouselInner.innerHTML = '';
    indicators.innerHTML = '';
    
    // 添加轮播图项目
    newsData.carousel.forEach((item, index) => {
        const slide = document.createElement('div');
        slide.className = 'carousel-item';
        slide.innerHTML = `
            <img src="${item.image}" alt="${item.title}">
            <div class="carousel-content">
                <h3>${item.title}</h3>
                <p>${item.summary}</p>
            </div>
        `;
        carouselInner.appendChild(slide);
        
        // 添加指示器
        const indicator = document.createElement('div');
        indicator.className = `indicator ${index === 0 ? 'active' : ''}`;
        indicator.onclick = () => goToSlide(index);
        indicators.appendChild(indicator);
    });
    
    // 自动轮播
    setInterval(() => {
        changeSlide(1);
    }, 5000);
}

function changeSlide(direction) {
    currentSlide = (currentSlide + direction + totalSlides) % totalSlides;
    updateCarousel();
}

function goToSlide(index) {
    currentSlide = index;
    updateCarousel();
}

function updateCarousel() {
    const carouselInner = document.querySelector('.carousel-inner');
    carouselInner.style.transform = `translateX(-${currentSlide * 100}%)`;
    
    // 更新指示器
    document.querySelectorAll('.indicator').forEach((indicator, index) => {
        indicator.classList.toggle('active', index === currentSlide);
    });
}

// 加载新闻列表
function loadNews(category = 'all') {
    const newsGrid = document.getElementById('news-grid');
    const loading = document.getElementById('loading');
    
    // 显示加载状态
    loading.style.display = 'block';
    newsGrid.innerHTML = '';
    
    // 模拟加载延迟
    setTimeout(() => {
        loading.style.display = 'none';
        
        let filteredNews = newsData.news;
        if (category !== 'all' && category !== 'home') {
            filteredNews = newsData.news.filter(news => news.category.toLowerCase() === category);
        }
        
        filteredNews.forEach(news => {
            const newsCard = createNewsCard(news);
            newsGrid.appendChild(newsCard);
        });
        
        // 如果没有新闻
        if (filteredNews.length === 0) {
            newsGrid.innerHTML = '<p style="text-align: center; color: #666; padding: 2rem;">暂无相关新闻</p>';
        }
    }, 500);
}

// 创建新闻卡片
function createNewsCard(news) {
    const card = document.createElement('div');
    card.className = 'news-card';
    card.onclick = () => {
        window.location.href = `#detail?id=${news.id}`;
    };
    
    card.innerHTML = `
        <img src="${news.image}" alt="${news.title}" class="news-card-image">
        <div class="news-card-content">
            <h3 class="news-card-title">${news.title}</h3>
            <p class="news-card-summary">${news.summary}</p>
            <div class="news-card-meta">
                <span class="news-card-time">${news.time}</span>
                <span class="news-card-category">${news.category}</span>
            </div>
        </div>
    `;
    
    return card;
}

// 导航功能
function initNavigation() {
    const navLinks = document.querySelectorAll('.nav-link');
    
    navLinks.forEach(link => {
        link.addEventListener('click', (e) => {
            e.preventDefault();
            
            // 移除所有active类
            navLinks.forEach(l => l.classList.remove('active'));
            
            // 添加active类到当前链接
            link.classList.add('active');
            
            // 获取分类并加载新闻
            const category = link.getAttribute('data-category');
            loadNews(category);
        });
    });
}

// 移动端菜单切换
function initMobileMenu() {
    const mobileToggle = document.querySelector('.mobile-menu-toggle');
    const navList = document.querySelector('.nav-list');
    
    mobileToggle.addEventListener('click', () => {
        navList.classList.toggle('show');
    });
}

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', () => {
    initCarousel();
    initNavigation();
    initMobileMenu();
    loadNews('home');
});

// 响应式处理
window.addEventListener('resize', () => {
    const navList = document.querySelector('.nav-list');
    if (window.innerWidth > 768) {
        navList.classList.remove('show');
    }
});
